<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="食材名称" prop="ingredientName">
        <el-input
          v-model="queryParams.ingredientName"
          placeholder="请输入食材名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="category">
        <el-select v-model="queryParams.category" placeholder="请选择类别" clearable>
          <el-option label="谷物类" value="谷物类" />
          <el-option label="豆类" value="豆类" />
          <el-option label="蔬菜类" value="蔬菜类" />
          <el-option label="水果类" value="水果类" />
          <el-option label="肉类" value="肉类" />
          <el-option label="水产类" value="水产类" />
          <el-option label="蛋奶类" value="蛋奶类" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['diet:ingredient:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['diet:ingredient:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['diet:ingredient:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ingredientList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="食材ID" align="center" prop="ingredientId" />
      <el-table-column label="食材名称" align="center" prop="ingredientName" />
      <el-table-column label="类别" align="center" prop="category">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.category }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="可食部分(%)" align="center" prop="ediblePortion" />
      <el-table-column label="热量(千卡/100g)" align="center" prop="calories" />
      <el-table-column label="蛋白质(g/100g)" align="center" prop="protein" />
      <el-table-column label="脂肪(g/100g)" align="center" prop="fat" />
      <el-table-column label="碳水化合物(g/100g)" align="center" prop="carbohydrate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['diet:ingredient:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['diet:ingredient:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['diet:ingredient:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改食材对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="食材名称" prop="ingredientName">
              <el-input v-model="form.ingredientName" placeholder="请输入食材名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类别" prop="category">
              <el-select v-model="form.category" placeholder="请选择类别">
                <el-option label="谷物类" value="谷物类" />
                <el-option label="豆类" value="豆类" />
                <el-option label="蔬菜类" value="蔬菜类" />
                <el-option label="水果类" value="水果类" />
                <el-option label="肉类" value="肉类" />
                <el-option label="水产类" value="水产类" />
                <el-option label="蛋奶类" value="蛋奶类" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="可食部分(%)" prop="ediblePortion">
              <el-input-number v-model="form.ediblePortion" :min="0" :max="100" :precision="1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="热量(千卡/100g)" prop="calories">
              <el-input-number v-model="form.calories" :min="0" :precision="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="蛋白质(g/100g)" prop="protein">
              <el-input-number v-model="form.protein" :min="0" :precision="1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="脂肪(g/100g)" prop="fat">
              <el-input-number v-model="form.fat" :min="0" :precision="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="碳水化合物(g/100g)" prop="carbohydrate">
          <el-input-number v-model="form.carbohydrate" :min="0" :precision="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 食材详情对话框 -->
    <el-dialog title="食材详情" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="食材ID">{{ viewForm.ingredientId }}</el-descriptions-item>
        <el-descriptions-item label="食材名称">{{ viewForm.ingredientName }}</el-descriptions-item>
        <el-descriptions-item label="类别">{{ viewForm.category }}</el-descriptions-item>
        <el-descriptions-item label="可食部分">{{ viewForm.ediblePortion }}%</el-descriptions-item>
        <el-descriptions-item label="热量">{{ viewForm.calories }}千卡/100g</el-descriptions-item>
        <el-descriptions-item label="蛋白质">{{ viewForm.protein }}g/100g</el-descriptions-item>
        <el-descriptions-item label="脂肪">{{ viewForm.fat }}g/100g</el-descriptions-item>
        <el-descriptions-item label="碳水化合物">{{ viewForm.carbohydrate }}g/100g</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listIngredient, getIngredient, delIngredient, addIngredient, updateIngredient, exportIngredient } from "@/api/diet/ingredient";

export default {
  name: "DietIngredient",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      ingredientList: [],
      title: "",
      open: false,
      viewOpen: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ingredientName: null,
        category: null
      },
      form: {},
      viewForm: {},
      rules: {
        ingredientName: [
          { required: true, message: "食材名称不能为空", trigger: "blur" }
        ],
        category: [
          { required: true, message: "类别不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listIngredient(this.queryParams).then(response => {
        this.ingredientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        ingredientId: null,
        ingredientName: null,
        category: null,
        ediblePortion: null,
        calories: null,
        protein: null,
        fat: null,
        carbohydrate: null
      };
      this.resetForm("form");
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ingredientId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加食材";
    },
    handleUpdate(row) {
      this.reset();
      const ingredientId = row.ingredientId || this.ids
      getIngredient(ingredientId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改食材";
      });
    },
    handleView(row) {
      const ingredientId = row.ingredientId;
      getIngredient(ingredientId).then(response => {
        this.viewForm = response.data;
        this.viewOpen = true;
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ingredientId != null) {
            updateIngredient(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIngredient(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {
      const ingredientIds = row.ingredientId || this.ids;
      this.$modal.confirm('是否确认删除食材编号为"' + ingredientIds + '"的数据项？').then(function() {
        return delIngredient(ingredientIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport() {
      this.download('diet/ingredient/export', {
        ...this.queryParams
      }, `ingredient_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>